package com.heima.apuser.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.apuser.service.ApUserService;
import com.heima.feign.WemediaClient;
import com.heima.file.MinIOTemplate;
import com.heima.model.apuser.dto.ApUserRealnameDto;
import com.heima.model.apuser.entity.ApUser;
import com.heima.model.apuser.entity.ApUserRealname;
import com.heima.apuser.dao.ApUserRealnameDao;
import com.heima.apuser.service.ApUserRealnameService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemeda.entity.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * APP实名认证信息表(ApUserRealname)表服务实现类
 *
 * @author makejava
 * @since 2022-04-23 17:48:15
 */
@Service("apUserRealnameService")
public class ApUserRealnameServiceImpl implements ApUserRealnameService {
    @Resource
    private ApUserRealnameDao apUserRealnameDao;

    @Resource
    private MinIOTemplate minIOTemplate;

    @Resource
    private WemediaClient wemediaClient;

    @Resource
    private ApUserService apUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ApUserRealname queryById(Long id) {
        return this.apUserRealnameDao.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param apUserRealname 实例对象
     * @return 实例对象
     */
    @Override
    public ApUserRealname insert(ApUserRealname apUserRealname) {
        this.apUserRealnameDao.insert(apUserRealname);
        return apUserRealname;
    }

    /**
     * 修改数据
     *
     * @param apUserRealname 实例对象
     * @return 实例对象
     */
    @Override
    public ApUserRealname update(ApUserRealname apUserRealname) {
        this.apUserRealnameDao.update(apUserRealname);
        return this.queryById(apUserRealname.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.apUserRealnameDao.deleteById(id) > 0;
    }

    //上传用户信息
    @Override
    public ResponseResult submit(MultipartFile font_image,
                                 MultipartFile back_image,
                                 MultipartFile hold_image,
                                 MultipartFile live_image,
                                 String name,
                                 String idno) throws IOException {
        if (StringUtils.isBlank(idno) || StringUtils.isBlank(idno)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUserRealname apUserRealname = new ApUserRealname();

        //将用户照片上传到minio中
        if (font_image != null) {
            String font_imageFilename = font_image.getOriginalFilename();//图片名字
            String substring = font_imageFilename.substring(font_imageFilename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString();
            font_imageFilename = uuid + substring;
            String font_image_url = minIOTemplate.uploadImgFile("", font_imageFilename, font_image.getInputStream());
            apUserRealname.setFontImage(font_image_url);
        }

        if (back_image != null) {
            String back_imageFilename = back_image.getOriginalFilename();//图片名字
            String substring = back_imageFilename.substring(back_imageFilename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString();
            back_imageFilename = uuid + substring;
            String back_image_url = minIOTemplate.uploadImgFile("", back_imageFilename, back_image.getInputStream());
            apUserRealname.setBackImage(back_image_url);
        }

        if (hold_image != null) {
            String hold_imageFilename = hold_image.getOriginalFilename();//图片名字
            String substring = hold_imageFilename.substring(hold_imageFilename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString();
            hold_imageFilename = uuid + substring;
            String hold_image_url = minIOTemplate.uploadImgFile("", hold_imageFilename, hold_image.getInputStream());
            apUserRealname.setHoldImage(hold_image_url);
        }

        if (live_image != null) {
            String live_imageFilename = live_image.getOriginalFilename();//图片名字
            String substring = live_imageFilename.substring(live_imageFilename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString();
            live_imageFilename = uuid + substring;
            String live_image_url = minIOTemplate.uploadImgFile("", live_imageFilename, live_image.getInputStream());
            apUserRealname.setLiveImage(live_image_url);
        }

        apUserRealname.setUserId(WmThreadLocalUtil.get());
        apUserRealname.setName(name);
        apUserRealname.setIdno(idno);
        apUserRealname.setStatus(1);
        apUserRealname.setCreatedTime(new Date());
        apUserRealname.setUpdatedTime(new Date());
        apUserRealnameDao.insert(apUserRealname);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }


    //查询用户
    @Override
    public ResponseResult listAll(ApUserRealnameDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        PageHelper.startPage(dto.getPage(), dto.getSize());
        Page<ApUserRealname> page = apUserRealnameDao.queryAllByStatus(dto.getStatus());
        PageResponseResult pageResponseResult = new PageResponseResult(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getResult());
        return pageResponseResult;
    }

    //修改数据库审核状态
    @Override
    @GlobalTransactional
    public ResponseResult updataByStatus(ApUserRealnameDto dto, int i) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        apUserRealnameDao.updateByStatus(dto.getId(), dto.getMsg(), i);
        //如果用户审核通过 把他添加到自媒体端用户表里
        if (i == 9) {
            ApUser apUser = apUserService.queryById(WmThreadLocalUtil.get());
            WmUser wmUser = new WmUser();
            BeanUtils.copyProperties(apUser, wmUser);
            wmUser.setNickname(apUser.getNicknme());
            wemediaClient.save(wmUser);
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}


